package com.ykt.validator;

import com.jfinal.core.Controller;
import com.ykt.common.config.CommonConfigs;

/**
 * 上传API参数校验
 * @author long.hua
 * @version 1.0.0
 * @since 2016-01-17 21:11
 */
public class UploadValidator extends BaseValidator{

    private static String bizTypeNames[] = null;
    private static String fileTypeNames[] = null;

    static {
        init();
    }

    private static void init(){
        CommonConfigs.UploadBizType[] bizTypes = CommonConfigs.UploadBizType.values();
        bizTypeNames = new String[bizTypes.length];
        for (int i = 0; i < bizTypes.length; i++) {
            CommonConfigs.UploadBizType bizType = bizTypes[i];
            bizTypeNames[i] = bizType.name();
        }

        CommonConfigs.UploadFileType[] fileTypes = CommonConfigs.UploadFileType.values();
        fileTypeNames = new String[fileTypes.length];
        for (int i = 0; i < fileTypes.length; i++) {
            CommonConfigs.UploadFileType fileType = fileTypes[i];
            fileTypeNames[i] = fileType.name();
        }
    }

    @Override
    protected void validate(Controller c) {
        c.getFiles(); // 若是上传文件类型的post提交，要先运行这句，防止取不到数据

        validateRequiredString("bizType", "bizTypeMsg", "必须传业务类型！");
        validateRequiredString("fileType", "fileTypeMsg", "必须传文件类型！");

        validateInSet("bizType", bizTypeNames, "bizTypeMsg", "业务类型参数值错误！");
        validateInSet("fileType", fileTypeNames, "fileTypeMsg", "文件类型参数值错误！");
    }

    @Override
    protected void handleError(Controller c) {
        c.renderJson(super.toJsonText(c));
    }

}
